/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt.zaliczeniowy.tkunit.runner.TestOrderStrategy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import projekt.zaliczeniowy.tkunit.test.Test;

/**
 *
 * @author Tomek
 */
public class RandomStrategy implements Strategy {
    
    private static List<Test> shuffleList(List<Test> list) {
        List<Test> copy = new LinkedList<Test>(list);
        Collections.shuffle(copy);
        return copy;
    }

    public Iterator<Test> getTestOrder(final List<Test> tests) {
        return new SimpleTestIteratorTemplateMethodAdapter(shuffleList(tests)) {
            
            @Override
            public synchronized Test next() {
                Test test = null;
                if (hasMoreElements) {
                    test = testsToMahshall.get(currentIndex);
                    currentIndex++;
                    if (currentIndex >= testsToMahshall.size()) {
                        hasMoreElements = false;
                    }
                } else {
                    throw new Error("There is no more test!!! You didn't use the iterator properly");
                }
                return test;
            }
        };
    }
}
